﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ColdBootAutofac
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			var builder = new Autofac.Builder.ContainerBuilder();
			builder.Register<IWebApp>(
				c => new WebApp(
					c.Resolve<IAuthenticator>(),
					c.Resolve<IStockQuote>()))
				.FactoryScoped();

			builder.Register<IAuthenticator>(
				c => new Authenticator(
					c.Resolve<ILogger>(),
					c.Resolve<IErrorHandler>(),
					c.Resolve<IDatabase>()))
				.FactoryScoped();

			builder.Register<IStockQuote>(
				c => new StockQuote(
					c.Resolve<ILogger>(),
					c.Resolve<IErrorHandler>(),
					c.Resolve<IDatabase>()))
				.FactoryScoped();

			builder.Register<IDatabase>(
				c => new Database(
					c.Resolve<ILogger>(),
					c.Resolve<IErrorHandler>()))
				.FactoryScoped();

			builder.Register<IErrorHandler>(
				c => new ErrorHandler(c.Resolve<ILogger>()))
				.FactoryScoped();

			builder.Register<ILogger>(c => new Logger());

			var container = builder.Build();

			Action action = () =>
			{
				var webApp = container.Resolve<IWebApp>();
				webApp.Run();
			};

			action();

			Close();
		}
	}
}